package com.galaxy.device.api;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.galaxy.common.core.domain.R;
import com.galaxy.common.core.validate.AddGroup;
import com.galaxy.common.core.validate.EditGroup;
import com.galaxy.common.log.annotation.Log;
import com.galaxy.common.log.enums.BusinessType;
import com.galaxy.common.mybatis.core.page.PageQuery;
import com.galaxy.common.mybatis.core.page.TableDataInfo;
import com.galaxy.device.domain.DeviceBase;
import com.galaxy.device.domain.bo.DeviceBaseBo;
import com.galaxy.device.domain.vo.DeviceBaseVo;
import com.galaxy.device.service.IDeviceBaseService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备基础信息Controller
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/base")
public class DeviceBaseApi {

    private final IDeviceBaseService deviceBaseService;

    /**
     * 查询设备基础信息列表
     */
    @GetMapping("/list")
    public TableDataInfo<DeviceBaseVo> list(DeviceBaseBo bo, PageQuery pageQuery) {
        return deviceBaseService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取设备基础信息详细信息
     * type == 1是查详情，0是查单独
     */
    @GetMapping("/{id}/{type}")
    public R<DeviceBaseVo> getInfo(@PathVariable("id") Long id,@PathVariable("type") Long type) {
        DeviceBaseVo deviceBaseVo = deviceBaseService.queryVoById(id, type);
        return R.ok(deviceBaseVo);
    }

    @GetMapping("/getDeviceNo/{id}")
    public R<String> getDeviceNo(@PathVariable("id") Long id) {
        return R.ok("ok",deviceBaseService.getDeviceNo(id));
    }

    @GetMapping("/ByDeviceNo/{deviceNo}")
    public R<DeviceBaseVo> getInfoByNo(@PathVariable("deviceNo") String deviceNo) {
        DeviceBaseVo deviceBaseVo = deviceBaseService.getInfoByNo(deviceNo);
        return R.ok(deviceBaseVo);
    }

    /**
     * 新增设备基础信息
     */
    @Log(title = "设备基础信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Boolean> add(@Validated(AddGroup.class) @RequestBody DeviceBaseBo bo) {
        return R.ok(deviceBaseService.insertByBo(bo));
    }

    /**
     * 修改设备基础信息
     */
    @Log(title = "设备基础信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Boolean> edit(@Validated(EditGroup.class) @RequestBody DeviceBaseBo bo) {
        return R.ok(deviceBaseService.updateByBo(bo));
    }

    /**
     * 修改设备状态
     */
    @Log(title = "设备基础信息", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatusByDeviceNo")
    public R<Boolean> updateStatusByDeviceNo(@RequestBody DeviceBaseBo bo) {
        return R.ok(deviceBaseService.updateStatusByDeviceNo(bo));
    }

    /**
     * 修改设备离线
     */
    @Log(title = "设备离线", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatusOfflineByDeviceNo/{deviceID}")
    public R<Boolean> updateOfflineByDeviceNo(@PathVariable Long deviceID) {
        return R.ok(deviceBaseService.updateOfflineByDeviceNo(deviceID));
    }

    /**
     * 删除设备基础信息
     */
    @Log(title = "设备基础信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Boolean> remove(@PathVariable Long[] ids) {
        return R.ok(deviceBaseService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 设备总数添加
     */
    @Log(title = "设备基础信息", businessType = BusinessType.DELETE)
    @GetMapping("updateCount/{id}/{count}")
    public R<Boolean> updateCount(@PathVariable("id") Long id, @PathVariable("count") Integer count) {
        return R.ok(deviceBaseService.updateCount(id,count));
    }

    /**
     * 查询设备基础信息列表
     */
    @GetMapping("/listDevice")
    public R<List<DeviceBaseVo>> list(DeviceBaseBo bo) {
        return R.ok(deviceBaseService.queryList(bo));
    }

    /**
     * 获取设备总数（实时）
     */
    @GetMapping("/total")
    public R<Long> getTotalDevices() {
        return R.ok(deviceBaseService.getTotalDevices());
    }

    /**
     * 获取在线设备数（实时）
     */
    @GetMapping("/online")
    public R<Long> getOnlineDevices() {
        return R.ok(deviceBaseService.getOnlineDevices());
    }
}
